package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.EquipSpareInbound;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipSpareInboundDao;
import com.efast.dmom.mes.dao.entity.WmsSpareOrderLogDao;
import com.efast.dmom.mes.entity.EquipSpareEntity;
import com.efast.dmom.mes.entity.EquipSpareInboundEntity;
import com.efast.dmom.mes.entity.EquipSpareStorageEntity;
import com.efast.dmom.mes.entity.WmsSpareOrderLogEntity;
import com.efast.dmom.mes.service.EquipSpareInboundService;
import com.efast.dmom.mes.service.EquipSpareService;
import com.efast.dmom.mes.service.WmsEquipSpareStorageService;
import com.efast.dmom.mes.service.WmsSpareOutInboundLogService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderLogStatus;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;

@Service
@Transactional
public class EquipSpareInboundServiceImpl implements EquipSpareInboundService {

	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;

	@Autowired
	private EquipSpareInboundDao equipSpareInboundDao;

	@Autowired
	private WmsSpareOrderLogDao wmsSpareOrderLogDao;

	@Autowired
	private WmsSpareOutInboundLogService wmsSpareOutInboundLogService;

	@Autowired
	private EquipSpareService equipSpareService;

	@Autowired
	private WmsEquipSpareStorageService wmsEquipSpareStorageService;

	@Override
	public ResultBean save(EquipSpareInbound spareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> spareList = spareInbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			String orderNo = "";
			boolean flag = true;
			String queueMessage = " ";
			String storageMessage = " ";
			String inboundStyle = spareList.get(0).getInboundStyle();
			for (EquipSpareInboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("刀具") && entity.getCutterType().equals("刀具-有编号")) {
					EquipSpareInboundEntity inboundEntity = equipSpareInboundDao
							.selectSpareInboundBycutterCode(entity.getSpareId(), WmsOrderStatusUtil.NOCONFIRM);
					int qty = 0;
					if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS) {
						qty = equipSpareService.getInventoryByCutterCode(entity.getSpareId());
					} else {
						qty = equipSpareService.getQtyByCutterCode(entity.getSpareId());
					}
					if (inboundEntity != null) {
						flag = false;
						if (queueMessage.equals(" ")) {
							queueMessage = "入库队列中已存在刀具编号为“" + entity.getCutterCode();
						} else {
							queueMessage = queueMessage.concat("、" + entity.getCutterCode());
						}
					}
					if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS && qty > 0) {
						flag = false;
						if (storageMessage.equals(" ")) {
							storageMessage = "库存中已存在刀具编号为“" + entity.getCutterCode();
						} else {
							storageMessage = queueMessage.concat("、" + entity.getCutterCode());
						}
					} else if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_RECEIVE_STATUS && qty > 1) {
						flag = false;
						if (storageMessage.equals(" ")) {
							storageMessage = "库存中已存在刀具编号为“" + entity.getCutterCode();
						} else {
							storageMessage = queueMessage.concat("、" + entity.getCutterCode());
						}
					} else {
						if (qty > 1) {
							flag = false;
							if (storageMessage.equals(" ")) {
								storageMessage = "库存中已存在刀具编号为“" + entity.getCutterCode();
							} else {
								storageMessage = queueMessage.concat("、" + entity.getCutterCode());
							}
						}
					}
				}
			}
			if (!flag) {
				resultBean.setSuccess(false);
				String message = "";
				if (!queueMessage.equals(" ")) {
					if (!storageMessage.equals(" ")) {
						message = "1:".concat(queueMessage + "”的刀具！" + "\n");
					} else {
						message = queueMessage + "”的刀具!";
					}
				}
				if (!storageMessage.equals(" ")) {
					if (!queueMessage.equals(" ")) {
						message = message.concat("2:" + storageMessage + "”的刀具!");
					} else {
						message = storageMessage + "”的刀具!";
					}
				}
				resultBean.setMessage(message);
				return resultBean;
			}
			if (inboundStyle.equals("备品备件")) {
				orderNo = serviceBaseEntityUtil.getNo(WmsOrderStatusUtil.SPAREINBOUNDCODE);
			} else {
				orderNo = serviceBaseEntityUtil.getNo(WmsOrderStatusUtil.CUTTERINBOUNDCODE);
			}
			for (EquipSpareInboundEntity entity : spareList) {
				entity.setInboundNumber(orderNo);
				entity.setInboundStatus(WmsOrderStatusUtil.NOCONFIRM);
				entity.setCreationTime(TimeUtil.getLocalDateTime());
				entity.setCreator(SpringWebUtils.getRequestUser());
				entity.setCompanyCode(SpringWebUtils.getRequestCompany());
				entity.setRowStatus(1);
			}
			equipSpareInboundDao.saveBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.ADD.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(EquipSpareInbound spareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> spareList = spareInbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareInboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				equipSpareInboundDao.update(entity);
			}
			addSpareOperationLog(spareList, WmsOrderLogStatus.UPDATE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("更新成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(EquipSpareInbound spareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> list = spareInbound.getSpareList();
		if (list != null && list.size() > 0) {
			List<EquipSpareInboundEntity> spareList = equipSpareInboundDao.getSpareInboundList(list);
			for (EquipSpareInboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
			}
			equipSpareInboundDao.deleteBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.DELETE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除的数据为空!");
		}
		return resultBean;
	}

	public void addSpareOperationLog(List<EquipSpareInboundEntity> spareList, String operation) {
		if (spareList != null && spareList.size() > 0) {
			List<WmsSpareOrderLogEntity> logList = new ArrayList<WmsSpareOrderLogEntity>();
			for (EquipSpareInboundEntity entity : spareList) {
				WmsSpareOrderLogEntity logEntity = new WmsSpareOrderLogEntity();
				logEntity.setOutInboundOrderId(entity.getId());
				logEntity.setOutInboundOrderNo(entity.getInboundNumber());
				logEntity.setOrderOperationStatus(operation);
				logEntity.setCreationTime(TimeUtil.getLocalDateTime());
				logEntity.setCreator(SpringWebUtils.getRequestUser());
				logEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
				logList.add(logEntity);
			}
			wmsSpareOrderLogDao.saveBatch(logList);
		}
	}

	// 备件入库确认
	@Override
	public ResultBean affirmSpareInBound(List<EquipSpareInboundEntity> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list != null && list.size() > 0) {
			List<EquipSpareInboundEntity> spareList = equipSpareInboundDao.getSpareInboundList(list);
			for (EquipSpareInboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				entity.setInboundStatus(WmsOrderStatusUtil.CONFIRM);
				entity.setInboundTime(TimeUtil.getLocalDateTime());
				entity.setInboundUser(SpringWebUtils.getRequestUser());
			}
			equipSpareInboundDao.affirmSpareInBound(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.CONFIRM.getOperation());
			wmsSpareOutInboundLogService.addSpareInboundLog(spareList, WmsOrderStatusUtil.INWAREHOUSE);

			List<EquipSpareStorageEntity> inboundList = new ArrayList<EquipSpareStorageEntity>();
			List<EquipSpareEntity> equipSpareList = new ArrayList<EquipSpareEntity>();
			for (EquipSpareInboundEntity data : spareList) {
				EquipSpareStorageEntity stotage = new EquipSpareStorageEntity();
				EquipSpareEntity spare = new EquipSpareEntity();
				int inboundQuantity = 0;
				int inboundQty = 0;
				if (data.getSpareCategory().equals("刀具") && data.getCutterType().equals("刀具-有编号")) {
					if (data.getStorageQuality() == 1) {
						inboundQuantity = 0;
						inboundQty = 0;
						stotage.setFlag(false);
					} else {
						stotage.setFlag(true);
						for (EquipSpareInboundEntity entity : spareList) {
							if (data.getSpareId() == entity.getSpareId()) {
								inboundQuantity = inboundQuantity + entity.getInboundQuantity();
							}
							if (data.getSpareId() == entity.getSpareId()
									&& data.getWarehouseId() == entity.getWarehouseId()) {
								inboundQty = inboundQty + entity.getInboundQuantity();
							}
						}
					}
				} else {
					stotage.setFlag(true);
					for (EquipSpareInboundEntity entity : spareList) {
						if (data.getSpareId() == entity.getSpareId()) {
							inboundQuantity = inboundQuantity + entity.getInboundQuantity();
						}
						if (data.getSpareId() == entity.getSpareId()
								&& data.getWarehouseId() == entity.getWarehouseId()) {
							inboundQty = inboundQty + entity.getInboundQuantity();
						}
					}
				}
				stotage.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
				spare.setUsedStatus(WmsOrderStatusUtil.SPARE_ENABLE_STATUS);
				stotage.setWarehouseId(data.getWarehouseId());
				stotage.setSpareId(data.getSpareId());
				stotage.setTotalAmount(data.getUnitPrice() * inboundQty);
				spare.setId(data.getSpareId());
				spare.setLastModifiedTime(TimeUtil.getLocalDateTime());
				spare.setLastModifier(SpringWebUtils.getRequestUser());
				stotage.setStorageQuality(inboundQty);
				stotage.setSpareCategory(data.getSpareCategory());
				stotage.setCutterType(data.getCutterType());
				spare.setInventory(inboundQuantity);
				inboundList.add(stotage);
				equipSpareList.add(spare);
			}
			for (int i = 0; i < inboundList.size() - 1; i++) {
				for (int j = inboundList.size() - 1; j > i; j--) {
					if (inboundList.get(j).getSpareId() == inboundList.get(i).getSpareId()
							&& inboundList.get(j).getWarehouseId() == inboundList.get(i).getWarehouseId()) {
						inboundList.remove(j);
					}
				}
			}
			for (int i = 0; i < equipSpareList.size() - 1; i++) {
				for (int j = equipSpareList.size() - 1; j > i; j--) {
					if (equipSpareList.get(j).getId() == equipSpareList.get(i).getId()) {
						equipSpareList.remove(j);
					}
				}
			}
			wmsEquipSpareStorageService.increaseSpareStorage(inboundList);
			equipSpareService.increaseInventoryBySpareId(equipSpareList);
			resultBean.setSuccess(true);
			resultBean.setMessage("确认成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("确认的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean saveItem(EquipSpareInbound equipSpareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> spareList = equipSpareInbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareInboundEntity entity : spareList) {
				if (entity.getSpareCategory().equals("刀具")) {
					if (entity.getCutterType().equals("刀具-有编号")) {
						EquipSpareInboundEntity inboundEntity = equipSpareInboundDao
								.selectSpareInboundBycutterCode(entity.getSpareId(), WmsOrderStatusUtil.NOCONFIRM);
						int qty = 0;
						if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS) {
							qty = equipSpareService.getInventoryByCutterCode(entity.getSpareId());
						} else {
							qty = equipSpareService.getQtyByCutterCode(entity.getSpareId());
						}
						if (inboundEntity != null) {
							resultBean.setSuccess(false);
							resultBean.setMessage("入库队列中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
							return resultBean;
						}
						if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS && qty > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
							return resultBean;
						} else if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_RECEIVE_STATUS && qty > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
							return resultBean;
						} else {
							if (qty > 1) {
								resultBean.setSuccess(false);
								resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
								return resultBean;
							}
						}
					} else if (entity.getCutterType().equals("刀具-无编号")) {
						List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
						if (inboundList != null && inboundList.size() > 0) {
//							for (EquipSpareInboundEntity data : inboundList) {
//								if (data.getSpareId() == entity.getSpareId()
//										&& data.getWarehouseId() == entity.getWarehouseId()) {
							resultBean.setSuccess(false);
							resultBean.setMessage("该备件入库单号下已存在入仓库为" + entity.getWarehouseName() + "无刀具编号的刀具！");
							return resultBean;
//								}
//							}
						}
					}
				} else {
					List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
					if (inboundList != null && inboundList.size() > 0) {
//						for (EquipSpareInboundEntity data : inboundList) {
//							if (data.getSpareId() == entity.getSpareId()
//									&& data.getWarehouseId() == entity.getWarehouseId()) {
						resultBean.setSuccess(false);
						resultBean.setMessage("该备件入库单号下已存在入仓库为" + entity.getWarehouseName() + "的备件！");
						return resultBean;
//							}
//						}
					}
				}
				entity.setInboundStatus(WmsOrderStatusUtil.NOCONFIRM);
				entity.setCreationTime(TimeUtil.getLocalDateTime());
				entity.setCreator(SpringWebUtils.getRequestUser());
				entity.setCompanyCode(SpringWebUtils.getRequestCompany());
				entity.setRowStatus(1);
			}
			equipSpareInboundDao.saveBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.ADD.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("保存成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean updateItem(EquipSpareInbound equipSpareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> spareList = equipSpareInbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareInboundEntity entity : spareList) {
				EquipSpareInboundEntity beforeUpdateData = equipSpareInboundDao.getById(entity.getId());
				if (beforeUpdateData.getSpareId() != entity.getSpareId()) {
					if (entity.getSpareCategory().equals("刀具")) {
						if (entity.getCutterType().equals("刀具-有编号")) {
							EquipSpareInboundEntity inboundEntity = equipSpareInboundDao
									.selectSpareInboundBycutterCode(entity.getSpareId(), WmsOrderStatusUtil.NOCONFIRM);
							int qty = 0;
							if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS) {
								qty = equipSpareService.getInventoryByCutterCode(entity.getSpareId());
							} else {
								qty = equipSpareService.getQtyByCutterCode(entity.getSpareId());
							}
							if (inboundEntity != null) {
								resultBean.setSuccess(false);
								resultBean.setMessage("入库队列中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
								return resultBean;
							}
							if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_ENABLE_STATUS && qty > 0) {
								resultBean.setSuccess(false);
								resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
								return resultBean;
							} else if (entity.getUsedStatus() == WmsOrderStatusUtil.SPARE_RECEIVE_STATUS && qty > 0) {
								resultBean.setSuccess(false);
								resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
								return resultBean;
							} else {
								if (qty > 1) {
									resultBean.setSuccess(false);
									resultBean.setMessage("库存中已存在刀具编号为“" + entity.getCutterCode() + "”的刀具！");
									return resultBean;
								}
							}
						} else if (entity.getCutterType().equals("刀具-无编号")) {
							List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
							if (inboundList != null && inboundList.size() > 0) {
								resultBean.setSuccess(false);
								resultBean.setMessage("该备件入库单号下已存在入仓库为“" + entity.getWarehouseName() + "”无刀具编号的刀具！");
								return resultBean;
							}
						}
					} else if (entity.getSpareCategory().equals("备品备件")) {
						List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
						if (inboundList != null && inboundList.size() > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("该备件入库单号下已存在入“" + entity.getWarehouseName() + "”仓库的备件！");
							return resultBean;
						}
					}
				}
				if (beforeUpdateData.getSpareId() == entity.getSpareId()
						&& beforeUpdateData.getWarehouseId() != entity.getWarehouseId()) {
					if (entity.getSpareCategory().equals("刀具") && entity.getCutterType().equals("刀具-无编号")) {
						List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
						if (inboundList != null && inboundList.size() > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("该备件入库单号下已存在入仓库为" + entity.getWarehouseName() + "无刀具编号的刀具！");
							return resultBean;
						}
					} else if (entity.getSpareCategory().equals("备品备件")) {
						List<EquipSpareInboundEntity> inboundList = equipSpareInboundDao.getInboundList(entity);
						if (inboundList != null && inboundList.size() > 0) {
							resultBean.setSuccess(false);
							resultBean.setMessage("该备件入库单号下已存在入仓库为“" + entity.getWarehouseName() + "”的备件！");
							return resultBean;
						}
					}
				}
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
				equipSpareInboundDao.update(entity);
			}
			addSpareOperationLog(spareList, WmsOrderLogStatus.UPDATE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("更新成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的数据为空!");
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteItem(EquipSpareInbound equipSpareInbound) {
		ResultBean resultBean = new ResultBean(true);
		List<EquipSpareInboundEntity> spareList = equipSpareInbound.getSpareList();
		if (spareList != null && spareList.size() > 0) {
			for (EquipSpareInboundEntity entity : spareList) {
				entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
				entity.setLastModifier(SpringWebUtils.getRequestUser());
			}
			equipSpareInboundDao.deleteBatch(spareList);
			addSpareOperationLog(spareList, WmsOrderLogStatus.DELETE.getOperation());
			resultBean.setSuccess(true);
			resultBean.setMessage("删除成功!");
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除的数据为空!");
		}
		return resultBean;
	}

}
